package com.lshj;

import com.hj.lib.util.HjTioUtil;
import com.hj.lib.util.LogUtil;
import com.lshj.impl.HjTioImpl;
import com.teamdev.jxbrowser.chromium.BrowserContext;
import com.teamdev.jxbrowser.chromium.BrowserContextParams;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import potato.jx.crack.JxBrowserHackUtil;
import potato.jx.crack.JxVersion;

import java.io.File;

import static com.hj.lib.util.TioConfig.home;
import static com.hj.lib.util.TioConfig.jxmh;


public class QmMain extends Application {
    public  static com.teamdev.jxbrowser.chromium.Browser browser = null;
    public  static  Stage primaryStage = null;
    public  static  String qmUrl = "";
    @Override
    public void start(final Stage primaryStage) {

        BrowserContext context = new BrowserContext(new BrowserContextParams(jxmh+ File.separator+"qm"));
        browser = new com.teamdev.jxbrowser.chromium.Browser(context);
        BrowserView view = new BrowserView(browser);
        Scene scene = new Scene(new BorderPane(view));
        primaryStage.setFullScreen(true);
        primaryStage.setAlwaysOnTop(false);
        primaryStage.setScene(scene);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.show();
        primaryStage.setIconified(true);
        primaryStage.setAlwaysOnTop(false);
        System.out.println(qmUrl);
        HjTioUtil.startClient(new HjTioImpl());
        browser.loadURL(qmUrl);
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            public void handle(WindowEvent event) {
                browser.dispose();
                Platform.exit();
            }
        });
        QmMain.primaryStage = primaryStage;

        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1000 * 7);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Platform.runLater(new Runnable() {
                    public void run() {
                        QmMain.primaryStage.setMaximized(true);
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        QmMain.primaryStage.setIconified(true);
                    }
                });
            }
        });
        thread.start();
    }

    public static void main(String[] args) {

        try {
            JxBrowserHackUtil.hack(JxVersion.V6_22);
            if (args != null) {
                LogUtil.debug("QmMain",args.length+"");
                for (int i = 0; i < args.length; i++) {
                    LogUtil.debug("QmMain",args[i]);
                }
            }
            qmUrl = args[0];
            QmMain.launch(args);
        } catch (Exception e) {
            qmUrl = "";
            QmMain.launch(args);
        }
    }
}
